{% import 'Dockerfile/docker.jinja2'    as docker %}
{% import 'Dockerfile/provision.jinja2' as provision %}
{% import 'Dockerfile/services.jinja2'  as services %}

{% macro alpine(role='') -%}
    # Install php environment
    && apk-install \
        # Install tools
        imagemagick \
        graphicsmagick \
        ghostscript \
        jpegoptim \
        pngcrush \
        libjpeg-turbo-utils \
        optipng \
        pngquant \
        # Install php (cli/fpm)
        php7-fpm \
        php7-json \
        php7-intl \
        php7-curl \
        php7-mysqli \
        php7-mysqlnd \
        php7-pdo_mysql \
        php7-pdo_pgsql \
        php7-pdo_sqlite \
        php7-mcrypt \
        php7-gd \
        # disabled until Imagick was compiled against Image Magick version 1799 but version 1800 is loaded is fixed
        php7-imagick \
        php7-imap \
        php7-bcmath \
        php7-soap \
        php7-sqlite3 \
        php7-bz2 \
        php7-calendar \
        php7-ctype \
        php7-mongodb \
        php7-pcntl \
        php7-pgsql \
        php7-posix \
        php7-sockets \
        php7-sysvmsg \
        php7-sysvsem \
        php7-sysvshm \
        php7-xmlreader \
        php7-exif \
        php7-ftp \
        php7-gettext \
        php7-iconv \
        php7-zip \
        php7-zlib \
        php7-shmop \
        php7-wddx \
        sqlite \
        php7-xmlrpc \
        php7-xsl \
        geoip \
        php7-ldap \
        # php7-memcache \
        php7-redis \
        php7-pear \
        php7-phar \
        php7-openssl \
        php7-session \
        php7-opcache \
        php7-mbstring \
        php7-iconv \
        php7-apcu \
        php7-fileinfo \
        php7-simplexml \
        php7-tokenizer \
        php7-xmlwriter \
        php7-pecl-amqp \
    && ln -s /usr/sbin/php-fpm7 /usr/sbin/php-fpm \
    && pecl channel-update pecl.php.net \
    # Temporarily disable pear due to https://twitter.com/pear/status/1086634389465956352
    # && pear channel-update pear.php.net \
    # && pear upgrade-all \
    && pear config-set auto_discover 1 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer2 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --1 \
    && ln -sf /usr/local/bin/composer2 /usr/local/bin/composer \
    # PECL workaround, see webdevops/Dockerfile#78
    && sed -i "s/ -n / /" $(which pecl) \
    {{ services.php() }}
{%- endmacro %}

{% macro debianSury(distribution,role='') -%}
    && apt-install apt-transport-https lsb-release \
    && echo "deb https://packages.sury.org/php/ {{ distribution }} main" >> /etc/apt/sources.list \
    && echo "deb http://ftp2.de.debian.org/debian/ testing main" >> /etc/apt/sources.list \
    && echo "deb-src http://ftp2.de.debian.org/debian/ testing main" >> /etc/apt/sources.list \
    && wget -O- https://packages.sury.org/php/apt.gpg | apt-key add - \
    && echo "Package: *" > /etc/apt/preferences.d/debian_testing.pref \
    && echo "Pin: origin ftp2.de.debian.org" >> /etc/apt/preferences.d/debian_testing.pref \
    && echo "Pin-Priority: -10" >> /etc/apt/preferences.d/debian_testing.pref \
    && echo "Package: libpcre3" > /etc/apt/preferences.d/libpcre.pref \
    && echo "Pin: release a=testing" >> /etc/apt/preferences.d/libpcre.pref \
    && echo "Pin-Priority: 995" >> /etc/apt/preferences.d/libpcre.pref \
    && apt-get update \
    && apt-get -t testing install -y -f libpcre3 \
    && apt-install \
        # Install tools
        imagemagick \
        graphicsmagick \
        ghostscript \
        jpegoptim \
        libjpeg-turbo-progs \
        pngcrush \
        optipng \
        apngopt \
        pngnq \
        pngquant \
        # Install php (cli/fpm)
        php7.0-cli \
        php7.0-fpm \
        php7.0-json \
        php7.0-intl \
        php7.0-curl \
        php7.0-mysql \
        php7.0-mcrypt \
        php7.0-gd \
        php7.0-imagick \
        php7.0-imap \
        php7.0-sqlite3 \
        php7.0-pgsql \
        php7.0-ldap \
        php7.0-opcache \
        php7.0-xmlrpc \
        php7.0-xsl \
        php7.0-bz2 \
        php7.0-redis \
        php7.0-memcached \
        php7.0-zip \
        php7.0-soap \
        php7.0-bcmath \
        php7.0-mbstring \
        php-mongodb \
        php-apcu \
        php-amqp \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer2 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --1 \
    && ln -sf /usr/local/bin/composer2 /usr/local/bin/composer \
    {{ services.php() }}
{%- endmacro %}

{% macro debian9(role='') -%}
    # Install php environment
    && apt-install \
        # Install tools
        imagemagick \
        graphicsmagick \
        ghostscript \
        jpegoptim \
        libjpeg-turbo-progs \
        pngcrush \
        optipng \
        apngopt \
        pngnq \
        pngquant \
        # Install php (cli/fpm)
        php7.0-cli \
        php7.0-fpm \
        php7.0-json \
        php7.0-intl \
        php7.0-curl \
        php7.0-mysql \
        php7.0-mcrypt \
        php7.0-gd \
        php7.0-imagick \
        php7.0-imap \
        php7.0-sqlite3 \
        php7.0-pgsql \
        php7.0-ldap \
        php7.0-opcache \
        php7.0-soap \
        php7.0-zip \
        php7.0-mbstring \
        php7.0-bcmath \
        php7.0-xmlrpc \
        php7.0-xsl \
        php7.0-bz2 \
        php-pear \
        php-apcu \
        php-redis \
        php-mongodb \
        php-memcache \
        php-memcached \
        php-amqp \
    && pecl channel-update pecl.php.net \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer2 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --1 \
    && ln -sf /usr/local/bin/composer2 /usr/local/bin/composer \
    {{ services.php() }}
{%- endmacro %}

{% macro debian10(role='') -%}
    # Install php environment
    && apt-install \
        # Install tools
        imagemagick \
        graphicsmagick \
        ghostscript \
        jpegoptim \
        libjpeg-turbo-progs \
        pngcrush \
        optipng \
        apngopt \
        pngnq \
        pngquant \
        # Install php (cli/fpm) | php always references the latest version
        php-cli \
        php-fpm \
        php-json \
        php-intl \
        php-curl \
        php-mysql \
        php-gd \
        php-imagick \
        php-imap \
        php-sqlite3 \
        php-pgsql \
        php-ldap \
        php-opcache \
        php-soap \
        php-zip \
        php-mbstring \
        php-bcmath \
        php-xmlrpc \
        php-xsl \
        php-bz2 \
        php-pear \
        php-apcu \
        php-redis \
        php-mongodb \
        php-memcache \
        php-memcached \
    && pecl channel-update pecl.php.net \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer2 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --1 \
    && ln -sf /usr/local/bin/composer2 /usr/local/bin/composer \
    {{ services.php() }}
{%- endmacro %}

{% macro ubuntu(role='', phpVersion='7.0') -%}
    # Install php environment
    && apt-install \
        # Install tools
        imagemagick \
        graphicsmagick \
        ghostscript \
        jpegoptim \
        libjpeg-turbo-progs \
        pngcrush \
        optipng \
        apngopt \
        pngnq \
        pngquant \
        # Install php (cli/fpm)
        php{{ phpVersion }}-cli \
        php{{ phpVersion }}-fpm \
        php{{ phpVersion }}-json \
        php{{ phpVersion }}-intl \
        php{{ phpVersion }}-curl \
        php{{ phpVersion }}-mysql \
{%- if phpVersion == '7.0' %}
        php{{ phpVersion }}-mcrypt \
{%- endif %}
        php{{ phpVersion }}-gd \
        php{{ phpVersion }}-sqlite3 \
        php{{ phpVersion }}-imap \
        php{{ phpVersion }}-pgsql \
        php{{ phpVersion }}-ldap \
        php{{ phpVersion }}-opcache \
        php{{ phpVersion }}-soap \
        php{{ phpVersion }}-zip \
        php{{ phpVersion }}-mbstring \
        php{{ phpVersion }}-bcmath \
        php{{ phpVersion }}-xmlrpc \
        php{{ phpVersion }}-xsl \
        php{{ phpVersion }}-bz2 \
        php-pear \
        php-apcu \
        php-igbinary \
        php-mongodb \
        php-imagick \
        php-redis \
        php-amqp \
{%- if phpVersion == '7.1' %}
        php-libsodium \
{%- endif %}
        php-memcached \
    && pecl channel-update pecl.php.net \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer2 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --1 \
    && ln -sf /usr/local/bin/composer2 /usr/local/bin/composer \
    {{ services.php() }}
{%- endmacro %}

{% macro centosWebtatic(role='') -%}
    && rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm \
    && yum-install \
        ImageMagick \
        GraphicsMagick \
        ghostscript \
        jpegoptim \
        libjpeg-turbo-utils \
        optipng \
        pngcrush \
        pngnq \
        pngquant \
        # Install php (cli/fpm)
        php70w-cli \
        php70w-fpm \
        php70w-common \
        php70w-intl \
        php70w-imap \
        php70w-mysqlnd \
        php70w-pecl-memcached \
        php70w-mcrypt \
        php70w-gd \
        php70w-pgsql \
        php70w-mbstring \
        php70w-bcmath \
        php70w-soap \
        php70w-pecl-apcu \
        sqlite \
        php70w-xmlrpc \
        php70w-xml \
        geoip \
        php70w-ldap \
        ImageMagick-devel \
        ImageMagick-perl \
        php70w-pear \
        php70w-devel \
        gcc \
        make \
        php70w-opcache \
        php70w-pecl-imagick \
        php70w-pecl-mongodb \
    && pecl channel-update pecl.php.net \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer2 \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --1 \
    && ln -sf /usr/local/bin/composer2 /usr/local/bin/composer \
    && pecl install redis \
    && echo "extension=redis.so" > /etc/php.d/redis.ini \
    && yum remove -y ImageMagick-devel php70w-devel gcc make \
    {{ services.php() }}
{%- endmacro %}
